package com.tucc.hello.leetcode.no1_400.no1_200.no21_40.no21;

import com.tucc.hello.leetcode.common.ListNode;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode l1 = ListNode.of(2, 5, 8);
        ListNode l2 = ListNode.of(1, 6, 7);
        ListNode head = solution.mergeTwoLists(l1, l2);
        ListNode.print(head);

    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null && l2 == null){
            return null;
        }
        else if(l1 == null){
            return l2;
        }
        else if(l2 == null){
            return l1;
        }
        else if(l1.val <= l2.val){
           l1.next = mergeTwoLists(l1.next, l2);
           return l1;
        }
        else{
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }

    public void merge(ListNode head, ListNode l1, ListNode l2){
        if(l1 == null){
            head.next = l2;
            return;
        }
        else if(l2 == null){
            head.next = l1;
            return;
        }
        else if(l1.val <= l2.val){
            head.next = l1;
            l1 = l1.next;
        }
        else{
            head.next = l2;
            l2 = l2.next;
        }
        merge(head.next, l1, l2);
    }

}


